from django.db import models


class Project(models.Model):
    """
        Blockly项目代码
    """

    LANGUAGE_CHOICES = [
        [-1, 'Other'],
        [0, 'Python'],
        [1, 'JavaScript']
    ]

    title = models.CharField(verbose_name='项目名称', max_length=32, null=False, blank=False, unique=True)
    code = models.TextField(verbose_name='代码内容', null=False, blank=False)
    object_code = models.TextField(verbose_name='转换后的代码', null=False, blank=False)
    object_code_language = models.IntegerField(verbose_name='转换后的代码语言', choices=LANGUAGE_CHOICES, default=0)
    add_time = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    def __str__(self):
        return f'[{self.get_object_code_language_display()}] [{self.title}]'

    class Meta:
        verbose_name = 'Blockly项目'
        verbose_name_plural = verbose_name


class UserDefinedBlocklyBlock(models.Model):
    """
        用户自定义Blockly块
    """

    LANGUAGE_CHOICES = [
        [-1, 'Other'],
        [0, 'Python'],
        [1, 'JavaScript']
    ]

    name = models.CharField(verbose_name='blockly块名', max_length=32, unique=True, null=False, blank=False)
    code = models.TextField(verbose_name='代码内容', null=False, blank=False)
    code_language = models.IntegerField(verbose_name='代码语言', choices=LANGUAGE_CHOICES, default=0)
    add_time = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    def __str__(self):
        return f'[{self.get_code_language_display()}] {self.name}'

    class Meta:
        verbose_name = 'Blockly自定义函数'
        verbose_name_plural = verbose_name


class CodeTemplate(models.Model):
    """
        代码模板，用于合成最终可执行的代码
    """

    name = models.CharField(verbose_name='模板名', max_length=32, unique=True, null=False, blank=False)
    content = models.TextField(verbose_name='代码内容', null=False, blank=False)
    add_time = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    def __str__(self):
        return f'[{self.name}]'

    class Meta:
        verbose_name = '代码模板'
        verbose_name_plural = verbose_name
